const path = require('path')
const PlatformPlugin = require('less-plugin-platform')

/**
 * 打印第三方依赖unhandledRejection错误问题日志
 */
process.on('unhandledRejection', (reason) => {
  console.log('Unhandled Rejection at: Promise', reason)
})

const resolve = _path => path.resolve(__dirname, '..', _path)

// 配置详见: https://taro-docs.jd.com/taro/docs/config-detail
const config = {
  // 项目名称
  projectName: 'taro-vue2',
  // 项目创建日期
  date: '2022-11-20',
  // 设计稿尺寸
  designWidth: 750,
  // 设计稿转换规则
  deviceRatio: {
    640: 2.34 / 2,
    750: 1,
    828: 1.81 / 2
  },
  // 项目源码目录
  sourceRoot: 'src',
  // 项目编译目录
  outputRoot: `dist/${process.env.TARO_ENV}`,
  // 编译插件配置
  plugins: [
    '@tarojs/plugin-html',
    path.join(__dirname, 'plugin-keep-ide-config.js')
  ],
  // 全局变量设置
  defineConstants: {
  },
  // 目录/文件别名
  alias: {
    '@': resolve('src'),
    axios$: 'axios/dist/axios.min.js'
  },
  // 文件copy配置
  copy: {
    patterns: [
    ],
    options: {
    }
  },
  // 框架, react, vue, vue3等
  framework: 'vue',
  compiler: {
    type: 'webpack5',
    prebundle: {
      exclude: ['vant']
    }
  },
  cache: {
    enable: true // Webpack 持久化缓存配置，建议开启。默认配置请参考：https://docs.taro.zone/docs/config-detail#cache
  },
  // 小程序专用配置
  mini: {
    postcss: {
      pxtransform: {
        enable: false,
        config: {
        }
      },
      url: {
        enable: true,
        config: {
          limit: 1024 // 设定转换尺寸上限
        }
      },
      cssModules: {
        enable: false, // 默认为 false，如需使用 css modules 功能，则设为 true
        config: {
          namingPattern: 'module', // 转换模式，取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]'
        }
      }
    },
    lessLoaderOption: {
      lessOptions: {
        modifyVars: {
          hack: `true;@import "~@/assets/less/variables.less"`
        },
        plugins: [new PlatformPlugin({ platform: process.env.TARO_ENV })]
      },
      additionalData: `@import "~@/assets/less/variables.less";@import "~@/assets/less/functions.less";`
    },
    enableSourceMap: process.env.NODE_ENV !== 'production',
    miniCssExtractPluginOption: {
      ignoreOrder: true
    },
    optimizeMainPackage: {
      enable: true
    },
    // 自定义webpack配置
    webpackChain(chain, webpack) {
      const needPxPluginRules = ['nomorlCss', 'less']
      needPxPluginRules.forEach(ruleName => {
        chain.module.rule(ruleName).oneOf('0').use('2').tap(options => {
          options.postcssOptions.plugins.push(require('@we125182/postcss-px-transform')({
            exclude: /[\\/]node_modules[\\/]vant/i
          }))
          return options
        })
      })

      /**
       * 开启打包依赖分析
       */
      // chain.plugin('analyzer').use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin, [])

      /**
       * 输出webpack配置
       */
      // require('fs').writeFileSync(resolve(`${process.env.TARO_ENV}.inspect.config.js`), 'module.exports = ' + chain.toString())
    },
    runtime: {
      // fix: 修复h5组件使用Element.contains报错问题,例如: van-checkbox
      enableContains: true
    },
    needClearOutput: false
  }
}

module.exports = function(merge) {
  if (process.env.NODE_ENV === 'development') {
    return merge({}, config, require('./dev'))
  }
  return merge({}, config, require('./prod'))
}
